package com.msh.automatic.android.control.script.task

import android.accessibilityservice.AccessibilityService
import android.content.ActivityNotFoundException
import android.content.Intent
import android.provider.Settings
import cn.hutool.core.util.StrUtil
import com.alibaba.fastjson2.JSONObject
import com.msh.automatic.android.control.script.service.ScriptAccessibilityService
import com.msh.automatic.android.control.script.util.AccessibilityServiceUtil
import com.msh.automatic.android.control.script.util.TaskApi
import com.msh.automatic.android.control.script.util.TaskLogStatus
import com.msh.automatic.android.control.script.util.TaskUtil
import kotlinx.coroutines.delay
import kotlin.system.exitProcess

class check_debugger_port(
    task: JSONObject, taskLog: JSONObject,token: String
) : Task(task, taskLog, token) {
    override suspend fun run(service: ScriptAccessibilityService): Boolean {
        val intent = Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            service.startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无法打开开发者选项:"+e.message);
            return false
        }
        if(!AccessibilityServiceUtil.waitActivity(service,"com.android.settings","com.android.settings.Settings\$DevelopmentSettingsDashboardActivity",3)){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"等待打开开发者选项界面失败");
            return false
        }
        if(!AccessibilityServiceUtil.clickByText(service,"无线调试",true)){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"开发者选项界面，点击【无线调试】失败")
            return false
        }
        delay(10_000)  // 30秒
        var nodes = AccessibilityServiceUtil.findAccessibilityNodeInfosByText(service,"连接到 WLAN 后启用调试模式")
        if(nodes.isEmpty()){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无线调试界面，找不到【连接到 WLAN 后启用调试模式】")
            return false
        }
        if(nodes.get(0).parent.childCount != 3){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无线调试界面，【连接到 WLAN 后启用调试模式】子元素数量错误："+nodes.get(0).parent.childCount)
            return false
        }
        var debug = nodes.get(0).parent.getChild(2).stateDescription
        if(debug == null){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无线调试界面，【连接到 WLAN 后启用调试模式】 状态错误：${debug}")
            return false
        }
        if(!StrUtil.equals("开启",debug)){
            if(!AccessibilityServiceUtil.clickByText(service,"连接到 WLAN 后启用调试模式")){
                TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无线调试界面，【连接到 WLAN 后启用调试模式】 状态：${debug}， 点击打开调试模式错误")
                return false
            }
        }
        nodes = AccessibilityServiceUtil.findAccessibilityNodeInfosByText(service,"IP 地址和端口")
        if(nodes.isEmpty()){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无线调试界面，找不到【IP 地址和端口】")
            return false
        }
        val address = nodes.get(0).parent.getChild(1).text
        val parts = address.split(":")
        val ip = parts[0]
        val port = parts[1].toInt()
        TaskApi.debugAddress(ip,port);
        TaskApi.log(service,this.getTaskLogId(),TaskLogStatus.over,"")
        return true
    }
}